function! api#vimtex#init() abort
    if !exists('g:loaded_vimtex')
        try
            let g:vimtex_enabled = v:true
            packadd vimtex
        catch /.*/
            echoerr v:exception
        endtry
    endif
    if exists("g:loaded_vimtex") && g:loaded_vimtex
        let g:conceallevel=0
        let g:tex_flavor = "latex"
        let g:vimtex_version_check = 0
        if empty(v:servername) && exists('*remote_startserver')
            call remote_startserver('VIM')
        endif
        call s:vimtex_mappings()
        call s:vimtex_compiler()
        call s:vimtex_viewer()
    endif
endfunction

function! s:vimtex_latexmk() abort
    let g:vimtex_compiler_latexmk = {
        \ 'aux_dir' : '',
        \ 'out_dir' : '',
        \ 'callback' : 1,
        \ 'continuous' : 1,
        \ 'executable' : 'latexmk',
        \ 'hooks' : [],
        \ 'options' : [
        \   '-verbose',
        \   '-file-line-error',
        \   '-synctex=1',
        \   '-interaction=nonstopmode',
        \ ],
        \}
endfunction

function! s:vimtex_arara() abort
    let g:vimtex_compiler_arara = {
        \ 'options' : ['--log'],
        \ 'hooks' : [],
        \}
endfunction

function! s:vimtex_compiler() abort
    let g:vimtex_compiler_enabled = 1
    let g:vimtex_compiler_method = "arara"
    " g:vimtex_compiler_latexmk
    " g:vimtex_compiler_latexmk_engines
    " g:vimtex_compiler_latexrun
    " g:vimtex_compiler_latexrun_engines
    " g:vimtex_compiler_tectonic
    " g:vimtex_compiler_arara
    " g:vimtex_compiler_generic
    " call s:vimtex_latexmk()
    call s:vimtex_arara()
endfunction

function! s:vimtex_viewer() abort
    let g:vimtex_view_method = "general"
    if executable("SumatraPDF")
        let g:vimtex_view_general_viewer = "SumatraPDF"
    endif
    if executable("evince")
        let g:vimtex_view_general_viewer = "evince"
    endif
endfunction

function! s:vimtex_mappings() abort    
    let g:maplocalleader = get(g:, "maplocalleader", ";")
    let g:vimtex_mappings_prefix = get(g:, "vimtex_mappings_prefix", "<localleader>l")
    let g:vimtex_mappings_enabled = 1
    nmap <localleader>li  <plug>(vimtex-info)
    nmap <localleader>lI  <plug>(vimtex-info-full)
    nmap <localleader>lt  <plug>(vimtex-toc-open)
    nmap <localleader>lT  <plug>(vimtex-toc-toggle)
    nmap <localleader>lq  <plug>(vimtex-log)
    nmap <localleader>lv  <plug>(vimtex-view)
    nmap <localleader>lr  <plug>(vimtex-reverse-search)
    nmap <localleader>ll  <plug>(vimtex-compile)
    nmap <localleader>lL  <plug>(vimtex-compile-selected)
    xmap <localleader>lL  <plug>(vimtex-compile-selected)
    nmap <localleader>lk  <plug>(vimtex-stop)
    nmap <localleader>lK  <plug>(vimtex-stop-all)
    nmap <localleader>le  <plug>(vimtex-errors)
    nmap <localleader>lo  <plug>(vimtex-compile-output)
    nmap <localleader>lg  <plug>(vimtex-status)
    nmap <localleader>lG  <plug>(vimtex-status-all)
    nmap <localleader>lc  <plug>(vimtex-clean)
    nmap <localleader>lC  <plug>(vimtex-clean-full)
    nmap <localleader>lm  <plug>(vimtex-imaps-list)
    nmap <localleader>lx  <plug>(vimtex-reload)
    nmap <localleader>lX  <plug>(vimtex-reload-state)
    nmap <localleader>ls  <plug>(vimtex-toggle-main)
    nmap <localleader>la  <plug>(vimtex-context-menu)
    " nmap dse              <plug>(vimtex-env-delete)
    " nmap dsc              <plug>(vimtex-cmd-delete)
    " nmap ds$              <plug>(vimtex-env-delete-math)
    " nmap dsd              <plug>(vimtex-delim-delete)
    " nmap cse              <plug>(vimtex-env-change)
    " nmap csc              <plug>(vimtex-cmd-change)
    " nmap cs$              <plug>(vimtex-env-change-math)
    " nmap csd              <plug>(vimtex-delim-change-math)
    " nmap tsf              <plug>(vimtex-cmd-toggle-frac)
    " xmap tsf              <plug>(vimtex-cmd-toggle-frac)
    " nmap tsc              <plug>(vimtex-cmd-toggle-star)
    " nmap tss              <plug>(vimtex-env-toggle-star)
    " nmap tse              <plug>(vimtex-env-toggle)
    " nmap ts$              <plug>(vimtex-env-toggle-math)
    " nmap tsb              <plug>(vimtex-env-toggle-break)
    " nmap <F6>             <plug>(vimtex-env-surround-line)
    " nmap                  <plug>(vimtex-env-surround-operator)
    " nmap <F6>             <plug>(vimtex-env-surround-visual)
    " nmap tsd              <plug>(vimtex-delim-toggle-modifier)
    " xmap tsd              <plug>(vimtex-delim-toggle-modifier)
    " nmap tsD              <plug>(vimtex-delim-toggle-modifier-reverse)
    " xmap tsD              <plug>(vimtex-delim-toggle-modifier-reverse)
    " nmap <F7>             <plug>(vimtex-cmd-create)
    " xmap <F7>             <plug>(vimtex-cmd-create)
    " imap <F7>             <plug>(vimtex-cmd-create)
    " imap ]]               <plug>(vimtex-delim-close)
    " nmap <F8>             <plug>(vimtex-delim-add-modifiers)
    " xmap ac               <plug>(vimtex-ac)
    " omap ac               <plug>(vimtex-ac)
    " xmap ic               <plug>(vimtex-ic)
    " omap ic               <plug>(vimtex-ic)
    " xmap ad               <plug>(vimtex-ad)
    " omap ad               <plug>(vimtex-ad)
    " xmap id               <plug>(vimtex-id)
    " omap id               <plug>(vimtex-id)
    " xmap ae               <plug>(vimtex-ae)
    " omap ae               <plug>(vimtex-ae)
    " xmap ie               <plug>(vimtex-ie)
    " omap ie               <plug>(vimtex-ie)
    " xmap a$               <plug>(vimtex-a$)
    " omap a$               <plug>(vimtex-a$)
    " xmap i$               <plug>(vimtex-i$)
    " omap i$               <plug>(vimtex-i$)
    " xmap aP               <plug>(vimtex-aP)
    " omap aP               <plug>(vimtex-aP)
    " xmap iP               <plug>(vimtex-iP)
    " omap iP               <plug>(vimtex-iP)
    " xmap am               <plug>(vimtex-am)
    " omap am               <plug>(vimtex-am)
    " xmap im               <plug>(vimtex-im)
    " omap im               <plug>(vimtex-im)
    " nmap %                <plug>(vimtex-%)
    " xmap %                <plug>(vimtex-%)
    " omap %                <plug>(vimtex-%)
    " nmap ]]               <plug>(vimtex-]])
    " xmap ]]               <plug>(vimtex-]])
    " omap ]]               <plug>(vimtex-]])
    " nmap ][               <plug>(vimtex-][)
    " xmap ][               <plug>(vimtex-][)
    " omap ][               <plug>(vimtex-][)
    " nmap []               <plug>(vimtex-[])
    " xmap []               <plug>(vimtex-[])
    " omap []               <plug>(vimtex-[])
    " nmap [[               <plug>(vimtex-[[)
    " xmap [[               <plug>(vimtex-[[)
    " omap [[               <plug>(vimtex-[[)
    " nmap ]m               <plug>(vimtex-]m)
    " xmap ]m               <plug>(vimtex-]m)
    " omap ]m               <plug>(vimtex-]m)
    " nmap ]M               <plug>(vimtex-]M)
    " xmap ]M               <plug>(vimtex-]M)
    " omap ]M               <plug>(vimtex-]M)
    " nmap [m               <plug>(vimtex-[m)
    " xmap [m               <plug>(vimtex-[m)
    " omap [m               <plug>(vimtex-[m)
    " nmap [M               <plug>(vimtex-[M)
    " xmap [M               <plug>(vimtex-[M)
    " omap [M               <plug>(vimtex-[M)
    " nmap ]n               <plug>(vimtex-]n)
    " xmap ]n               <plug>(vimtex-]n)
    " omap ]n               <plug>(vimtex-]n)
    " nmap ]N               <plug>(vimtex-]N)
    " xmap ]N               <plug>(vimtex-]N)
    " omap ]N               <plug>(vimtex-]N)
    " nmap [n               <plug>(vimtex-[n)
    " xmap [n               <plug>(vimtex-[n)
    " omap [n               <plug>(vimtex-[n)
    " nmap [N               <plug>(vimtex-[N)
    " xmap [N               <plug>(vimtex-[N)
    " omap [N               <plug>(vimtex-[N)
    " nmap ]r               <plug>(vimtex-]r)
    " xmap ]r               <plug>(vimtex-]r)
    " omap ]r               <plug>(vimtex-]r)
    " nmap ]R               <plug>(vimtex-]R)
    " xmap ]R               <plug>(vimtex-]R)
    " omap ]R               <plug>(vimtex-]R)
    " nmap [r               <plug>(vimtex-[r)
    " xmap [r               <plug>(vimtex-[r)
    " omap [r               <plug>(vimtex-[r)
    " nmap [R               <plug>(vimtex-[R)
    " xmap [R               <plug>(vimtex-[R)
    " omap [R               <plug>(vimtex-[R)
    " nmap ]/               <plug>(vimtex-]/)
    " xmap ]/               <plug>(vimtex-]/)
    " omap ]/               <plug>(vimtex-]/)
    " nmap ]*               <plug>(vimtex-]star)
    " xmap ]*               <plug>(vimtex-]star)
    " omap ]*               <plug>(vimtex-]star)
    " nmap [/               <plug>(vimtex-[/)
    " xmap [/               <plug>(vimtex-[/)
    " omap [/               <plug>(vimtex-[/)
    " nmap [*               <plug>(vimtex-[star)
    " xmap [*               <plug>(vimtex-[star)
    " omap [*               <plug>(vimtex-[star)
    " nmap K                <plug>(vimtex-doc-package)
    " --------------------------------------------------------------------- ~
endfunction
